package com.vecspace;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
	
/**重要！！！
 * 要将Application放在最外层，也就是要包含所有子包。
 * */

@Configuration
@ImportResource(locations={"classpath:spring.xml"})
@SpringBootApplication(exclude={RedisAutoConfiguration.class, RedisRepositoriesAutoConfiguration.class}) //自己配置redis链接，将系统自带的配置排除
public class AppStarter {
	public static void main(String[] args) {

	    SpringApplication.run(AppStarter.class, args);

	}
	
	@Bean("restTemplate")
    public RestTemplate restTemplate(){

	    RestTemplate restTemplate = new RestTemplate();
        
        FormHttpMessageConverter fc = new FormHttpMessageConverter();
        StringHttpMessageConverter stringConverter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        List<HttpMessageConverter<?>> partConverters = new ArrayList<HttpMessageConverter<?>>();
        partConverters.add(stringConverter);
        partConverters.add(new ResourceHttpMessageConverter());
        fc.setPartConverters(partConverters);
        List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
        messageConverters.clear();
        messageConverters.addAll(Arrays.asList(fc,new StringHttpMessageConverter()));
        return restTemplate;
    }
}
